import { isEmpty } from 'lodash';
import { defineStore } from 'pinia';
import useDictStore from './dict';
import { getDicts } from '../../api/system/dict/data';

export const useCustomStore = defineStore('custom', {
  state: () => ({
    customerType: [], // 客户类别数组
    addWeChat: [], // 是否添加微信状态数组
    afterSaleStatus: [], // 售后状态数组
    logisticsStatus: [], // 物流状态数组
    // 退货类型
    saleReturnType: []
  }),

  actions: {
    getDictsData(dictType) {
      return new Promise((resolve, reject) => {
        getDicts(dictType).then((resp) => {
          const dict = resp.data.map((p) => ({
            label: p.dictLabel,
            value: p.dictValue,
            elTagType: p.listClass,
            elTagClass: p.cssClass
          }));
          resolve(dict);
          useDictStore().setDict(dictType, dict);
        });
      });
    },
    // 退货类型
    async getSaleReturnType() {
      const dictType = 'sys_sales_return';
      try {
        const data = await this.getDictsData(dictType);
        this.saleReturnType = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取售后状态失败:', error);
        this.saleReturnType = [];
      }
    },
    // 售后类型
    async getAfterSaleStatus() {
      const dictType = 'after_sale_ststus';
      try {
        const data = await this.getDictsData(dictType);
        this.afterSaleStatus = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取售后状态失败:', error);
        this.afterSaleStatus = [];
      }
    },
    // 物流状态
    async getLogisticsStatus() {
      const dictType = 'sys_logistics_status';
      try {
        const data = await this.getDictsData(dictType);
        this.logisticsStatus = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取物流状态失败:', error);
        this.logisticsStatus = [];
      }
    },
    // 客户类别
    async getCustomerType() {
      const dictType = 'sys_user_type';
      try {
        const data = await this.getDictsData(dictType);
        this.customerType = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取客户类别失败:', error);
        this.customerType = [];
      }
    },
    // 是否添加微信
    async getHasWeChat() {
      const dictType = 'sys_add_wechat';
      try {
        const data = await this.getDictsData(dictType);
        this.addWeChat = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取是否添加微信失败:', error);
        this.addWeChat = [];
      }
    },

    // custom_types
    async getCustomerTypes() {
      const dictType = 'custom_types';
      try {
        const data = await this.getDictsData(dictType);
        this.customerTypes = Array.isArray(data) ? data : [];
      } catch (error) {
        console.error('获取客户类别失败:', error);
        this.customerTypes = [];
      }
    },

    // 初始化所有字典数据（可选：并行加载）
    async initCustom() {
      // 如果已经有值了不在请求
      if (isEmpty(this.customerType)) {
        await this.getCustomerType();
      }
      if (isEmpty(this.addWeChat)) {
        await this.getHasWeChat();
      }
      if (isEmpty(this.afterSaleStatus)) {
        await this.getAfterSaleStatus();
      }
      if (isEmpty(this.logisticsStatus)) {
        await this.getLogisticsStatus();
      }
      if (isEmpty(this.saleReturnType)) {
        await this.getSaleReturnType();
      }
      if (isEmpty(this.customerTypes)) {
        await this.getCustomerTypes();
      }
    },

    // 可选：单个刷新
    async refreshCustomerType() {
      await this.getCustomerType();
    },
    async refreshHasWeChat() {
      await this.getHasWeChat();
    }
  },

  // 可选：添加 getter，方便使用
  getters: {
    customerTypeOptions: (state) => {
      return state.customerType;
    },
    addWeChatOptions: (state) => {
      return state.addWeChat;
    },
    afterSaleStatusOptions: (state) => {
      return state.afterSaleStatus;
    },
    logisticsStatusOptions: (state) => {
      return state.logisticsStatus;
    },
    saleReturnTypeOptions: (state) => {
      return state.saleReturnType;
    },
    customerTypesOptions: (state) => {
      return state.customerTypes;
    }
  }
});
